package com.example.cart.adapter;

import static androidx.core.content.ContextCompat.startActivity;
import static com.example.cart.utils.GoodsDetailUtil.renderGoodsUnit;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.cart.GoodsDetailActivity;
import com.example.cart.MainActivity;
import com.example.cart.R;
import com.example.cart.aop.BitMapWorkerTask;
import com.example.cart.dto.Goods;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

public class GoodsListAdapter extends BaseAdapter implements View.OnClickListener {

    private final Context context;

    private List<Goods> goods;

    public GoodsListAdapter(Context context) {

        this.context = context;
        goods = Collections.emptyList();
    }

    public void update(List<Goods> goods) {
        this.goods = goods;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return goods.size();
    }

    @Override
    public Object getItem(int position) {
        return goods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return goods.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View goodsItemView;

        Holder holder;

        Goods good = goods.get(position);

        if (convertView == null) {

            holder = new Holder();

            goodsItemView = LayoutInflater.from(context).inflate(R.layout.activity_goods_unit, null);

            renderGoodsUnit(good, goodsItemView);
            holder.goodsName = goodsItemView.findViewById(R.id.goodsName);


            holder.goodsImage = goodsItemView.findViewById(R.id.goodsImage);

            holder.goodsDetail = goodsItemView.findViewById(R.id.goodsDetail);
            holder.goodsPrice = goodsItemView.findViewById(R.id.goodsPrice);

            goodsItemView.setTag(holder);
        } else {
            goodsItemView = convertView;
            holder = (Holder) goodsItemView.getTag();
        }

        holder.goodsName.setText(good.getName());

        new BitMapWorkerTask()
                .execute(
                        new BitMapWorkerTask.ImageSrcOperation(
                                good.getImageSrc(),
                                holder.goodsImage::setImageBitmap
                        )
                );
        /*holder.goodsImage.setImageBitmap(
                BitmapFactory.decodeFile(good.getImageSrc())
        );*/
        holder.goodsDetail.setText(good.getDetail());
        holder.goodsPrice.setText(good.getPrice());

        holder.goodsId = good.getId();

        goodsItemView.setOnClickListener(this);



        return goodsItemView;
    }

    @Override
    public void onClick(View v) {

        Holder holder = (Holder) v.getTag();

        Intent intent = new Intent(context, GoodsDetailActivity.class);
        intent.putExtra("goods_id",holder.goodsId);
        startActivity(context,intent,null);
    }

    static class Holder {
        public int goodsId;
        TextView goodsName;
        ImageView goodsImage;
        TextView goodsDetail;

        TextView goodsPrice;

    }
}
